/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package slicktest.util;

import java.util.ArrayList;
import java.util.List;

import slicktest.data.StatMod;
import slicktest.types.DamageType;

import slicktest.types.MathEffectType;

/**
 * utility class for turning an entry read in from a TagReader into usable types.
 * Everything is indexed from 1 because the first entry is the tag you got back.
 * @author hailstorm
 */
public class TagUtil {

    public static double marshallDouble(String[] doubleEntry) {
        double result = Double.NaN;
        try {
            result = Double.parseDouble(doubleEntry[1]);
        } catch (NumberFormatException nfe) {
            System.out.println("invalid double entry: " + doubleEntry[1]);
        }

        return result;
    }

    public static int marshallInt(String[] intEntry) {
        int result = Integer.MIN_VALUE;

        try {
            result = Integer.parseInt(intEntry[1]);
        } catch (NumberFormatException nfe) {
            System.out.println("invalid int entry: " + intEntry[1]);
        }

        return result;
    }

    public static String marshallString(String[] stringEntry) {
        String result = "";
        result = stringEntry[1];

        return result;
    }

    public static boolean marshallBoolean(String[] booleanEntry) {
        boolean result = false;
        result = Boolean.parseBoolean(booleanEntry[1]);

        return result;
    }

    public static StatMod marshallStatMod(String[] statModEntry) {
        System.out.println(statModEntry);
        StatMod result = null;

        double mod = Double.NaN;
        MathEffectType type = MathEffectType.NOOP;

        String modString = null;
        String typeString = null;

        if (statModEntry == null || statModEntry.length != 3) {
            System.out.println("tag entry is missing tokens, defaulting to NOOP");
            result = new StatMod(Double.NaN, MathEffectType.NOOP);
        } else {
            try {
                modString = statModEntry[1];
                typeString = statModEntry[2];

                mod = Double.parseDouble(modString);
                type = parseStatus(typeString);

                result = new StatMod(mod, type);

            } catch (NumberFormatException nfe) {
                System.out.println(modString + " is invalid as a decimal number, defaulting to NOOP");
                result = new StatMod(Double.NaN, MathEffectType.NOOP);
            }
        }

        return result;
    }

    private static MathEffectType parseStatus(String typeString) {
        MathEffectType result = MathEffectType.NOOP;

        if (typeString.equals("mult")) {
            result = MathEffectType.MULTIPLICATIVE;
        } else if (typeString.equals("add")) {
            result = MathEffectType.ADDITIVE;
        } else {
            System.out.println("Warning: " + typeString + " is not recognized. Defaulting to NOOP.");
        }

        return result;
    }

    public static List<DamageType> marshallDamageTypes(String[] typeEntry) {
        List<DamageType> result = new ArrayList<DamageType>();

        for (int i = 1; i < typeEntry.length; i++) {
            try {
                DamageType nextType = DamageType.valueOf(typeEntry[i]);
                result.add(nextType);
            } catch (IllegalArgumentException ex) {
                System.out.println("invalid damage type string: " + typeEntry[i]);
            }
        }

        return result;
    }

    public static List<String> marshallList(String[] listEntry) {
        List<String> result = new ArrayList<String>();

        if (listEntry != null) {
            for (int i = 1; i < listEntry.length; i++) {
                result.add(listEntry[i]);
            }
        } else {
            System.out.println("entry was null, you're getting an empty list back.");
        }

        return result;
    }

    public static boolean[][] marshallFootprint(String[] footEntry) {
        boolean[][] result = null;
        int xDim = 0;
        int yDim = 0;

        if (footEntry != null) {
            try {
                xDim = Integer.parseInt(footEntry[1]);
                yDim = Integer.parseInt(footEntry[2]);
                System.out.println("dimensions are valid: " + footEntry[1] + "," + footEntry[2]);
                result = new boolean[xDim][yDim];

                for (int y = 3; y < (3 + yDim); y++) {
                    String nextString = footEntry[y];
                    System.out.println("next: " + nextString);

                    for (int x = 0; x < xDim; x++) {
                        char nextChar = nextString.charAt(x);

                        switch (nextChar) {
                            case ('x'):
                                result[x][y - 3] = true;
                                break;
                            case ('o'):
                                result[x][y - 3] = false;
                                break;
                        }
                    }
                }
            } catch (NumberFormatException nfe) {
                System.out.println("dimensions are invalid: " + footEntry[1] + "," + footEntry[2]);
                System.out.println("using a default 1x1 footprint");
                result = new boolean[1][1];
                result[0][0] = true;
            }

        } else {
            result = new boolean[1][1];
            result[0][0] = true;
            System.out.println("no entry given, defaulting to 1x1 footprint.");
        }

        return result;
    }
}
